﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Reticulum
{
    public partial class Insert : Form
    {
        public Insert()
        {
            InitializeComponent();
        }
        public static Insert insert = new Insert();

        private void Insert_Load(object sender, EventArgs e)
        {
            //this.Height = 500;
            //this.Width = 800;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //判断用户是否存在,如果有则赋值给Account
            string sql2 = string.Format("select UserAccount from Users where UserAccount='{0}'", textBox3.Text);

            SqlDataReader reader = DBHelps.Select(sql2);

            string Account = null;

            while (reader.Read())
            {
                Account = reader["UserAccount"].ToString();
            }
            reader.Close();
            DBHelps.reader = null;

            if (textBox2.Text == "" || textBox3.Text == "")//判断输入是否为空
            {
                MessageBox.Show("账号和密码不能为空");
            }
            else if (textBox3.Text == Account)//判断注册的用户是否和数据库起冲突
            {
                MessageBox.Show("账号已存在请重新输入");
            }
            else if (textBox2.Text != "" && textBox3.Text != "")
            {
                string sql = string.Format("insert Users(UserName,UserPwd,UserAccount)" +
                "values('{0}', '{1}', '{2}')", textBox1.Text, textBox2.Text, textBox3.Text);

                bool result = DBHelps.Command(sql);

                if (result)
                {
                    MessageBox.Show("添加用户成功！");
                }
                else
                {
                    MessageBox.Show("添加用户失败！");
                }
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {
        }
    }
}
